package com.zzdx.deployment.web.controller.system;

import com.zzdx.deployment.common.annotation.Log;
import com.zzdx.deployment.common.base.AjaxResult;
import com.zzdx.deployment.common.constant.Constants;
import com.zzdx.deployment.common.enums.BusinessType;
import com.zzdx.deployment.common.page.TableDataInfo;
import com.zzdx.deployment.common.utils.ExcelUtil;
import com.zzdx.deployment.framework.util.ShiroUtils;
import com.zzdx.deployment.framework.web.base.BaseController;
import com.zzdx.deployment.system.domain.ProductResource;
import com.zzdx.deployment.system.service.IFileService;
import com.zzdx.deployment.system.service.IProductResourceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 产品资源 信息操作处理
 * 
 * @author mason
 * @date 2018-12-25
 */
@Controller
@RequestMapping("/system/productResource")
public class ProductResourceController extends BaseController
{
    private String prefix = "system/productResource";
	
	@Autowired
	private IProductResourceService productResourceService;
	@Autowired
	private IFileService fileService;

	@RequiresPermissions("system:productResource:view")
	@GetMapping()
	public String productResource()
	{
	    return prefix + "/productResource";
	}
	
	/**
	 * 查询产品资源列表
	 */
	@RequiresPermissions("system:productResource:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProductResource productResource)
	{
		startPage();
        List<ProductResource> list = productResourceService.selectProductResourceList(productResource);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出产品资源列表
	 */
	@RequiresPermissions("system:productResource:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductResource productResource)
    {
    	List<ProductResource> list = productResourceService.selectProductResourceList(productResource);
        ExcelUtil<ProductResource> util = new ExcelUtil<ProductResource>(ProductResource.class);
        return util.exportExcel(list, "productResource");
    }
	
	/**
	 * 新增产品资源
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 *  新增保存产品资源
	 * @param file 上传的文件
	 * @param productResource 资源对象信息
	 * @return
	 */
	@RequiresPermissions("system:productResource:add")
	@Log(title = "产品资源", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(@RequestParam("fileName") MultipartFile file,ProductResource productResource)
	{
		String loginName = ShiroUtils.getLoginName();
		productResource.setCreateBy(loginName);
		productResource.setCreateTime(new Date());
		String uploadResult = fileService.upload(file, productResource);
		return Constants.BOOLEAN_STRING_TRUE.equals(uploadResult)?success() : error();
	}

	/**
	 * 修改产品资源
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		ProductResource productResource = productResourceService.selectProductResourceById(id);
		mmap.put("productResource", productResource);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存产品资源
	 */
	@RequiresPermissions("system:productResource:edit")
	@Log(title = "产品资源", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(@RequestParam("fileName") MultipartFile file,ProductResource productResource)
	{
		String loginName = ShiroUtils.getLoginName();
		productResource.setUpdateBy(loginName);
		productResource.setUpdateTime(new Date());
		String uploadResult = fileService.updateAndUpload(file, productResource);
		return Constants.BOOLEAN_STRING_TRUE.equals(uploadResult)?success() : error();
	}
	
	/**
	 * 删除产品资源
	 */
	@RequiresPermissions("system:productResource:remove")
	@Log(title = "产品资源", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(productResourceService.deleteProductResourceByIds(ids));
	}
	
}
